// pages/rotate/rotate.js
var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    times: 0,
    canClick: true,
    rotateAng: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //yconsole.log(this);
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
        })
      }
    });
  },
  onShow: function () {


  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  rotate: function () {
   // console.log(wx.getStorageSync("oldTime"))
    if (wx.getStorageSync("oldTime")) {
      if (datedifference(wx.getStorageSync("oldTime"), util.formatTime(new Date)) >= 1) {
        wx.setStorageSync("oldTime", util.formatTime(new Date))
      } else {
        wx.showToast({
          title: '今天已经抽过奖了，明天再来',
        })
        return;
      }
    } else {
      wx.setStorageSync("oldTime", util.formatTime(new Date))
    }
    var that = this;
    that.data.times++;
    var rotateAng = (parseInt(Math.random() * 80) + parseInt(48)) * 22.5;
    that.setData({
      rotateAng: parseFloat(that.data.rotateAng) + parseFloat(rotateAng)
    })
    var str = that.data.rotateAng + '';
    if (str.indexOf(".") == -1) {
      that.data.rotateAng = parseFloat(that.data.rotateAng) + parseFloat(22.5)
    }

    var animation = wx.createAnimation({
      duration: 3000,
      timingFunction: "ease",
    })
    this.animation = animation;
    var _pink = parseFloat(that.data.rotateAng) % parseInt(360);
    var pinks = _pink / 22.5;
    setTimeout(function () {
      if (pinks == 1) {
        wx.showModal({
          title: '谢谢您的参与',
          content: '明日再接再厉！',
        })
      } else if (pinks == 3) {
        wx.showModal({
          title: '恭喜您中奖了',
          content: '请到我的-我的奖券里查看奖品！',
        })
        setStoragePink(3)
      } else if (pinks == 5) {
        wx.showModal({
          title: '恭喜您中奖了',
          content: '请到我的-我的奖券里查看奖品！',
        })
        setStoragePink(5)
      } else if (pinks == 7) {
        wx.showModal({
          title: '谢谢您的参与',
          content: '明日再接再厉！',
        })
      } else if (pinks == 9) {
        wx.showModal({
          title: '恭喜您中奖了',
          content: '请到我的-我的奖券里查看奖品！',
        })
        setStoragePink(9)
      } else if (pinks == 11) {
        wx.showModal({
          title: '恭喜您中奖了',
          content: '请到我的-我的奖券里查看奖品！',
        })
        setStoragePink(11)
      } else if (pinks == 13) {
        wx.showModal({
          title: '恭喜您中奖了',
          content: '请到我的-我的奖券里查看奖品！',
        })
        setStoragePink(13)
      } else if (pinks == 15) {
        wx.showModal({
          title: '恭喜您中奖了',
          content: '请到我的-我的奖券里查看奖品！',
        })
        setStoragePink(15)
      }
    }, 3600)
    animation.rotateZ(that.data.rotateAng).step();
    this.setData({
      animation: animation.export()
    })
  }
})


function datedifference(sDate1, sDate2) {    //sDate1和sDate2是2006-12-18格式  
  var dateSpan,
    tempDate,
    iDays;
  sDate1 = Date.parse(sDate1);
  sDate2 = Date.parse(sDate2);
  dateSpan = sDate2 - sDate1;
  dateSpan = Math.abs(dateSpan);
  iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
  return iDays
};

function setStoragePink(_id) {
  var arr = [];
  if (wx.getStorageSync("pinkArr")) {

    arr = wx.getStorageSync("pinkArr");
    if (arr.length > 10) {
      wx.showModal({
        title: '提示',
        content: '检测到您的奖券已经超过10张没有使用，请使用奖券过后再来抽奖！',
      })
      return;
    }
    arr.push({
      id: _id
    })
    wx.setStorageSync("pinkArr", arr)
  } else {
    arr.push({
      id: _id
    })
    wx.setStorageSync("pinkArr", arr)
  }
}